#include "includes.h"
#include "dialog.h"

extern HWND Window2;
HWND Window1;

/**
 * @函数功能：主窗口消息处理函数
 */ 
static LRESULT MiniGUIProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    HDC hdc;
    static int number = 0;

    switch (message) {
    case MSG_PAINT:                             /* 屏幕输出 */
        hdc = BeginPaint(hWnd);                 /* 获取设备上下文句柄 */
        TextOut(hdc, 0, 60, "Hello, World!");   /* 显示“Hello， World” */
        EndPaint(hWnd, hdc);                    /* 释放设备上下文句柄 */
        return 0;

    case MSG_CREATE:
        CreateWindow (CTRL_EDIT, "Edit Box 1",  /* 创建编辑框 */
        WS_CHILD | WS_VISIBLE | WS_BORDER,
        IDC_EDIT1, 75, 100, 150, 50, hWnd, 0);

        CreateWindow (CTRL_BUTTON, "openWindow_2", 
        WS_CHILD | BS_PUSHBUTTON | BS_CHECKED | WS_VISIBLE, 
        IDC_BUTTON_ONE, 75, 150, 150, 50, hWnd, 0); /* 创建按钮 */
        
        CreateWindow (CTRL_BUTTON, "openDialog", 
        WS_CHILD | BS_PUSHBUTTON | BS_CHECKED | WS_VISIBLE, 
        IDC_BUTTON_TWO, 75, 210, 150, 50, hWnd, 0); /* 创建按钮 */
        
        CreateWindow (CTRL_BUTTON, "openANonModalDialog", 
        WS_CHILD | BS_PUSHBUTTON | BS_CHECKED | WS_VISIBLE, 
        IDC_BUTTON_THREE, 75, 270, 150, 50, hWnd, 0); /* 创建按钮 */
        break;

    case MSG_COMMAND: /* 控件内部发生事件会给父窗口发送该事件 */
        if ((LOWORD(wParam) == IDC_EDIT1) && (HIWORD(wParam) == EN_CHANGE)) {
            printf("用户修改了子窗口 IDC_EDIT1 编辑框的内容, 现在做进一步处理...\n");
        }
        switch (wParam) {
        case IDC_BUTTON_ONE:
            WindowSecond(hWnd); /* 创建窗口二 */
            return 0;
        case IDC_BUTTON_TWO:
            CreateDialog(hWnd); /* 创建模态对话框 */
            return 0;
        case IDC_BUTTON_THREE:
            CreateANonModalDialog(hWnd);/* 创建非模态对话框 */
            return 0;
        }
        break;

    case MSG_CLOSE:
        printf("windows 1 MSG_CLOSE\r\n");
        DestroyMainWindow(hWnd);
        PostQuitMessage(hWnd);
        return 0;

    default:
        break;
    }
    return DefaultMainWinProc(hWnd, message, wParam, lParam);
}

int MiniGUIMain(int argc, char const *argv[])
{
    MSG Msg;
    MAINWINCREATE CreateInfo;

    #ifdef _MGRM_PROCESSES
    JoinLayer(NAME_DEF_LAYER, "MiniGUI", 0, 0);
    #endif

    CreateInfo.dwStyle = WS_VISIBLE | WS_MAXIMIZEBOX | WS_MINIMIZEBOX | WS_THINFRAME | WS_CAPTION;
    CreateInfo.dwExStyle = WS_EX_NONE;
    CreateInfo.spCaption = "Window-I";
    CreateInfo.hMenu = 0;
    CreateInfo.hCursor = GetSystemCursor(0);
    CreateInfo.hIcon = 0;
    CreateInfo.MainWindowProc = MiniGUIProc;
    CreateInfo.lx = 0;
    CreateInfo.ty = 0;
    CreateInfo.rx = 300;
    CreateInfo.by = 400;
    CreateInfo.iBkColor = COLOR_lightwhite;
    CreateInfo.dwAddData = 0;
    CreateInfo.hHosting = HWND_DESKTOP;

    Window1 = CreateMainWindow(&CreateInfo);
    if (Window1 == HWND_INVALID) {
        return 1;
    }

    ShowWindow(Window1, SW_SHOWNORMAL);

    while (GetMessage(&Msg, Window1)) {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }

    MainWindowThreadCleanup(Window1);

    return 0;
}

#ifdef _MGRM_THREADS
#include <minigui/dti.c>
#endif
